package physics.movement;

import model.objects.GameObject;
import model.objects.Level;
import physics.EuclideanVector;


/**
 * Defines the movement of AI characters which move horizontally back and forth
 * at a specified speed between two bounding x values.
 * 
 * @author geo4
 * 
 */
public class PatrolStrategy implements MovementStrategy {

    private double myLeftBound;
    private double myRightBound;
    private double mySpeed;

    /**
     * Creates a new instance of PatrolStrategy with the left and right bounds
     * on the x position of the GameObjects specified, and traveling with the
     * specified speed. If the GameObject is instantiated with an x-value
     * outside of these bounds, it will be forced into these bounds, but keeping
     * its y-component.
     * 
     * @param leftBound minimum x value for the farthest left point of the
     *        GameObject
     * @param rightBound maximum x value for the farthest right point of the
     *        GameObject
     * @param walkingSpeed speed at which the enemies should patrol
     */
    public PatrolStrategy (double leftBound, double rightBound, double walkingSpeed) {
        myLeftBound = leftBound;
        myRightBound = rightBound;
        mySpeed = walkingSpeed;
        if (myLeftBound > myRightBound) { throw new RuntimeException(
                                                                     "Patrol right bound must be greater than left bound"); }
    }

    @Override
    public void updatePosition (Level level, GameObject mover, double dt,
                                EuclideanVector ... globalForces) {
        mover.getVelocity().setLocation(mySpeed, 0);

        if (mover.getBounds().getX() <= myLeftBound) {
            mover.setPosition(myLeftBound, mover.getBounds().getY());
            mySpeed = -mySpeed;
        }
        else if (mover.getBounds().getMaxX() >= myRightBound) {
            mover.setPosition(myRightBound - mover.getBounds().getMaxX(), mover.getBounds().getY());
            mySpeed = -mySpeed;
        }
    }

}
